package cn.hellochaos.generator.service.impl;

import cn.hellochaos.generator.entity.CaipuSummary;
import cn.hellochaos.generator.entity.CaipuTips;
import cn.hellochaos.generator.mapper.CaipuTipsMapper;
import cn.hellochaos.generator.service.CaipuTipsService;
import cn.hellochaos.generator.utils.CheckObjectIsNullUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import cn.hellochaos.generator.exception.bizException.BizException;

import java.util.List;

/**
* <p>
* 小贴士 服务实现类
* </p>
*
* @author test
* @since 2021-01-23
*/
@Slf4j
@Service
public class CaipuTipsServiceImpl extends ServiceImpl<CaipuTipsMapper, CaipuTips> implements CaipuTipsService {

    @Autowired
    CaipuTipsMapper caipuTipsMapper;

    @Override
    public CaipuTips getCaipuTipByCookBookId(Integer caipuBookId) {
        QueryWrapper<CaipuTips> queryWrapper = new QueryWrapper<CaipuTips>();
        queryWrapper.eq("id_cookbook",caipuBookId);
        List<CaipuTips> caipuLists = caipuTipsMapper.selectList(queryWrapper);
        if (CheckObjectIsNullUtils.objCheckIsNull(caipuLists)){
            return new CaipuTips();
        }
        return new  CaipuTips();
    }

    @Override
    public Page<CaipuTips> listCaipuTipssByPage(int page, int pageSize, String factor) {
        log.info("正在执行分页查询caipuTips: page = {} pageSize = {} factor = {}",page,pageSize,factor);
        QueryWrapper<CaipuTips> queryWrapper =  new QueryWrapper<CaipuTips>().like("", factor);
        //TODO 这里需要自定义用于匹配的字段,并把wrapper传入下面的page方法
        Page<CaipuTips> result = super.page(new Page<>(page, pageSize));
        log.info("分页查询caipuTips完毕: 结果数 = {} ",result.getRecords().size());
        return result;
    }

    @Override
    public CaipuTips getCaipuTipsById(int id) {
        log.info("正在查询caipuTips中id为{}的数据",id);
        CaipuTips caipuTips = super.getById(id);
        log.info("查询id为{}的caipuTips{}",id,(null == caipuTips?"无结果":"成功"));
        return caipuTips;
    }

    @Override
    public int insertCaipuTips(CaipuTips caipuTips) {
        log.info("正在插入caipuTips");
        if (super.save(caipuTips)) {
            log.info("插入caipuTips成功,id为{}",caipuTips.getCaipuTipsId());
            return caipuTips.getCaipuTipsId();
        } else {
            log.error("插入caipuTips失败");
            throw new BizException("添加失败");
        }
    }

    @Override
    public int deleteCaipuTipsById(int id) {
        log.info("正在删除id为{}的caipuTips",id);
        if (super.removeById(id)) {
            log.info("删除id为{}的caipuTips成功",id);
            return id;
        } else {
            log.error("删除id为{}的caipuTips失败",id);
            throw new BizException("删除失败[id=" + id + "]");
        }
    }

    @Override
    public int updateCaipuTips(CaipuTips caipuTips) {
        log.info("正在更新id为{}的caipuTips",caipuTips.getCaipuTipsId());
        if (super.updateById(caipuTips)) {
            log.info("更新d为{}的caipuTips成功",caipuTips.getCaipuTipsId());
            return caipuTips.getCaipuTipsId();
        } else {
            log.error("更新id为{}的caipuTips失败",caipuTips.getCaipuTipsId());
            throw new BizException("更新失败[id=" + caipuTips.getCaipuTipsId() + "]");
        }
    }



}
